package com.ross.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Arrays;
import java.util.Date;

/**
 * @version 1.0.0
 * @className: ApiTestBubblingSort
 * @description: 冒泡排序
 * @author: zhangjunfa
 * @date: 2023/4/13 10:15
 */
@Slf4j
public class ApiTestBubblingSort {
    public static void main(String[] args) {
        int[] b = {49, 38, 65, 97, 76, 13, 27, 50,};
        test(b);
        // long start, end = 0L;
        // log.info("开始时间：{}", start = System.currentTimeMillis());
        // myBubblesort2(b);
        // log.info("结束时间：{}", end = System.currentTimeMillis());
        // System.out.println(Arrays.toString(b));
        // log.info("总共用时：{}", (end - start));
        // 结果：[13, 27, 38, 49, 50, 65, 76, 97]
        // 11 13 10 10 12 11 11 10 9 13 总共测试10次：平均用时：11

        //13 10  9  10 10 9  11 11 8 9  总共测试10次：平均用时：10
    }

    public static void test(int[] array) {
        long total = 0;
        for (int i = 0; i < 10; i++) {
            long start, end = 0L;
            start = System.currentTimeMillis();
            myBubblesort2(array);
            end = System.currentTimeMillis();
            total += (end - start);
        }
        log.info("总共测试：10次，平均用时：{}", total / 10);
    }


    public static void myBubblesort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = 0;
                    tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                }
            }
        }
    }

    /**
     * 趟数优化
     *
     * @param array
     */
    public static void myBubblesort2(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = 0;
                    tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                }
            }
        }
    }

    /**
     * 循环次数优化
     *
     * @param array
     */
    public static void myBubblesort3(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = 0;
                    tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                }
            }
        }
    }

    /**
     * 排序完成优化
     *
     * @param array
     */
    public static void myBubblesort4(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = 0;
                    tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flag = true;
                }
            }
            if (flag == false) {
                return;
            }
        }
    }
}
